<?php


namespace App\Jobs;


use App\Libraries\Queue\AMQPConsumer;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class XmlWriteQueue
    implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $xmlString;

    /**
     * Create a new job instance.
     *
     * @param $xmlString
     */
    public function __construct($xmlString)
    {
        $this->xmlString = $xmlString;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            $queue = config('customs.queue');
            $consumer = AMQPConsumer::instance($queue['host'], $queue['port'], $queue['user'], $queue['password'], $queue['vHost']);
            // 正式
//            $consumer->publisher(
//                base64_encode($this->xmlString),
//                $queue['declare'],
//                $queue['declare']
//            );
            // 测试

            $consumer->publisher(
                "<CEB622Message><InventoryReturn><guid>bcda7a96-9bb2-4e0a-ac5f-05dc944b1043</guid><customsCode>4921</customsCode>
    <ebpCode>4301364765</ebpCode>
    <ebcCode>4301364765</ebcCode>
    <agentCode>430166K046</agentCode>
    <copNo>{$this->xmlString}</copNo>
    <preNo>B20201124779212250</preNo>
    <invtNo>49212020I012906383</invtNo>
    <returnStatus>800</returnStatus>
    <returnTime>20201124170306134</returnTime>
    <returnInfo>[Code:2600;Desc:放行]</returnInfo>
  </InventoryReturn>
</CEB622Message>",
                $queue['declare'],
                $queue['declare']
            );
        }catch (Exception $exception){
            Log::error($exception->getMessage());
        }

    }

}
